package Servlets;

import Negocio.AdminDB;
import Negocio.Factura;
import Negocio.MessageLogger;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author asabido
 */
public class ListadoFacturas extends BaseServlet {

  RequestDispatcher rd;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
    */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException{

        HttpSession s = request.getSession(true);
        MessageLogger ml = new MessageLogger(getServletContext());
        Cookie[] cookies = request.getCookies();
        boolean login = false;

        for(int i=0; i<cookies.length; i++) {
          Cookie cookie = cookies[i];
          if (cookie.getName().equals("login") && cookie.getValue().equals("true"))
            login=true;
        }


        if(login){
            AdminDB adb = new AdminDB();
            Factura f = new Factura();
            List<Factura> facturas = new ArrayList<Factura>();
            String boton = request.getParameter("view");

            if(boton!=null)
            {
                if(boton.equalsIgnoreCase("view")){
                    try {
                        f = f.getByID(adb.getConnection(),Integer.parseInt(request.getParameter("idFactura")));
                        s.setAttribute("factura", f);
                        ml.log("'Admin' - visualizar factura "+f.getReferencia());
                        dispatch("/ViewFactura.jsp", request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger("ListadoFacturas").log(Level.SEVERE, null, ex);
                        dispatch("/404.jsp", request, response);
                    }
                }
            }
            else{
                try {
                        facturas = f.getAll(adb.getConnection());
                        s.setAttribute("listaDeFacturas", facturas);
                        ml.log("'Admin' - listado de facturas");
                        dispatch("/listaFacturas.jsp", request, response);                       
                    } catch (Exception ex) {
                        Logger.getLogger("ListadoFacturas").log(Level.SEVERE, null, ex);
                    }     
            }
        }
        else dispatch("/index.jsp", request, response);
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ListadoCitas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ListadoCitas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
